package ACWing.Dynamicprogramming;
//897. 最长公共子序列

import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/11/13 16:26
 */
public class TheIongestCommonSubsequence {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
//        String[]a=new String[n+1];
//        String[]b=new String[m+1];
//        for (int i = 1; i <=n; i++) {
//            a[i]=sc.next();
//        }
//        for (int i = 1; i <=m; i++) {
//            b[i]=sc.next();
//        }
        //这样的写法不能从1开始要判断边界条件；
        String a1=sc.next();
        String b1=sc.next();
        char[] a = a1.toCharArray();
        char[] b = b1.toCharArray();

        int[][]dp=new int[n+1][m+1];
        for (int i = 1; i <=n; i++) {
            for (int j = 1; j <=m; j++) {
                dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                if(a[i-1]==b[j-1]){
                    dp[i][j]=Math.max(dp[i][j],dp[i-1][j-1]+1);
                }
            }
        }
        System.out.println(dp[n][m]);
    }
}
